package com.hy.service.impl;

import com.hy.bean.Consumer;
import com.hy.bean.RespBean;
import com.hy.dao.impl.ConsumerDaoImpl;
import com.hy.service.ConsumerService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Consumer)表服务实现类
 *
 * @author makejava
 * @since 2024-04-29 11:08:28
 */
@Service("consumerService")
public class ConsumerServiceImpl implements ConsumerService {
//    @Resource
//    ConsumerDaoImpl consumerDao;
    private ConsumerDaoImpl consumerDao = new ConsumerDaoImpl();

    /**
     * 通过ID查询单条数据
     *
     * @param id
     * @return 实例对象
     */
    @Override
    public Consumer queryById(int id) {
        return this.consumerDao.queryById();
    }

    @Override
    public List<Consumer> queryAll() {
        return this.consumerDao.queryAll();
    }


    /**
     * 新增数据
     *
     * @param consumer 实例对象
     * @return 实例对象
     */
    @Override
    public Consumer insert(Consumer consumer) {
        this.consumerDao.insert(consumer);
        return consumer;
    }

    /**
     * 修改数据
     *
     * @param consumer 实例对象
     * @return 实例对象
     */
    @Override
    public Consumer update(Consumer consumer) {
        this.consumerDao.update(consumer);
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int id) {
        return false;
    }

    @Override
    public RespBean getConsumer(String username, String pwd, HttpServletRequest req, HttpServletResponse resp) {
        Consumer consumer = consumerDao.selectUsernameBy(username, pwd);
        if (consumer == null){
            return new RespBean(200,"error",null);
        }
        HttpSession session = req.getSession();
        session.setAttribute("id",consumer.getId());
        session.setAttribute("username",consumer.getUsername());

        return new RespBean(200,"ok",consumer);
    }
}
